package pe.edu.upla.lamerced.desk.view;

import java.awt.Dimension;
import java.awt.Frame;
import javax.swing.WindowConstants;
import pe.edu.upla.lamerced.Inicial;
import pe.edu.upla.lamerced.bean.Users;
import pe.edu.upla.lamerced.service.UsuarioService;

public class Login extends javax.swing.JFrame {

   private UsuarioService usuarioService;
   private static Users userLogin;

    /** Creates new form Login */
    public Login() {
        try {
            System.out.println("init");
            initComponents();
            System.out.println("traer usuario service");
            usuarioService = (UsuarioService)Inicial.getFactory().getBean("usuarioService");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtusername = new javax.swing.JTextField();
        txtpassword = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        btnCancel = new javax.swing.JButton();
        btnLogin = new javax.swing.JButton();
        lblMensaje = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setText("Usuario :");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(80, 40, 60, 15);

        txtusername.setText("44194490");
        getContentPane().add(txtusername);
        txtusername.setBounds(150, 40, 110, 20);

        txtpassword.setText("44194490");
        getContentPane().add(txtpassword);
        txtpassword.setBounds(150, 80, 110, 20);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel2.setText("Clave :");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(90, 80, 40, 15);

        btnCancel.setText("Cancelar");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        getContentPane().add(btnCancel);
        btnCancel.setBounds(190, 120, 90, 40);

        btnLogin.setText("Ingresar");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });
        getContentPane().add(btnLogin);
        btnLogin.setBounds(70, 120, 90, 40);

        lblMensaje.setForeground(new java.awt.Color(255, 0, 0));
        getContentPane().add(lblMensaje);
        lblMensaje.setBounds(0, 10, 340, 20);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
       dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
       // TODO add your handling code here:
       String usuario = txtusername.getText();
       String clave= "";
       if(txtpassword.getPassword()!=null){
         for(char car : txtpassword.getPassword()){
            clave = clave + car;
         }
       }
       
       System.out.println("clave = " + clave);
       Users busuario = usuarioService.autenticarUsuario(usuario,clave);
       if(busuario!=null){
          userLogin=busuario;
        Principal principal = new Principal();
        this.setVisible(false);
        principal.setVisible(true);
        lblMensaje.setText("");
       }else{
         lblMensaje.setText("Usuario o clave incorrectos");
       }
    }//GEN-LAST:event_btnLoginActionPerformed

     public void init() {
        System.out.println("METODO INIT");
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setSize(new Dimension(400, 250));
        setVisible(true);
        setState(Frame.NORMAL);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnLogin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel lblMensaje;
    private javax.swing.JPasswordField txtpassword;
    private javax.swing.JTextField txtusername;
    // End of variables declaration//GEN-END:variables

   
   public static Users getUserLogin() {
      return userLogin;
   }
   
}
